package com.ruoyi.crop.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crop.api.domain.Check;
import com.ruoyi.crop.api.domain.SrcLogistics;
import com.ruoyi.crop.api.domain.Warehouse;
import com.ruoyi.crop.api.model.RemoteUDriveService;
import com.ruoyi.crop.service.CheckService;
import com.ruoyi.crop.service.LogisticsService;
import com.ruoyi.crop.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author : Guosir
 * @date : 2024-07-27 10:41
 * @description :
 **/
@RestController
@RequestMapping("check")
public class CheckController extends BaseController {

    @Autowired
    private CheckService checkService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private LogisticsService logisticsService;
    @Autowired
    private RemoteUDriveService remoteUDriveService;

    /**
     * 查询质检列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Check check)
    {
        startPage();
        List<Check> list = checkService.selectCheckList(check);
        return getDataTable(list);
    }



    /**
     * 获取质检详细信息
     */
    @GetMapping(value = "/{checkId}")
    public AjaxResult getInfo(@PathVariable("checkId") Integer checkId) {
        return success(checkService.getById(checkId));
    }

    /**
     * 新增质检
     */
    @Log(title = "质检", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Check check) {
        check.setCheckCode("GG" + IdUtil.getSnowflakeNextIdStr());
        check.setCreateTime(new Date());
        boolean save = checkService.save(check);
        return toAjax(save);
    }

    /**
     * 修改质检
     */
    @Log(title = "质检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Check check) {
        check.setUpdateBy(SecurityUtils.getUsername());
        check.setUpdateTime(new Date());
        return toAjax(checkService.updateById(check));
    }

    /**
     * 删除质检
     */
    @Log(title = "质检", businessType = BusinessType.DELETE)
    @DeleteMapping("/{checkIds}")
    public AjaxResult remove(@PathVariable Integer[] checkIds) {
        return toAjax(checkService.removeBatchByIds(Arrays.asList(checkIds)));
    }



    @RequestMapping("addYunJiance")
    public AjaxResult addYunJiance(@RequestBody Check check){
        System.err.println(JSONUtil.toJsonStr(check));
        //通过仓库获取地址
        Warehouse warehouse = warehouseService.getOne(Wrappers.lambdaQuery(Warehouse.class).eq(Warehouse::getWarehouseId, check.getWarehouseId()));
        System.err.println(JSONUtil.toJsonStr(warehouse));
        SrcLogistics srcLogistics = new SrcLogistics();
        //目的地
        srcLogistics.setXid(warehouse.getXid());
        srcLogistics.setYid(warehouse.getYid());
        srcLogistics.setZid(warehouse.getZid());
        srcLogistics.setConsigner(SecurityUtils.getUsername());
        srcLogistics.setConsignee(warehouse.getPerson());
        srcLogistics.setMaterialLotId(check.getMaterialLotId());
        srcLogistics.setGoodsType(check.getProductName());
        srcLogistics.setGoodsNum(check.getCount());
        srcLogistics.setStartTime(check.getBeginTime());
        srcLogistics.setEndeTime(check.getEndTime());
        srcLogistics.setCreateBy(SecurityUtils.getUsername());
        srcLogistics.setRemark(check.getRemark());
        srcLogistics.setStatus(0);
        srcLogistics.setDriverId(check.getDriverId());
        String s = remoteUDriveService.getDriverById(check.getDriverId().longValue());
        System.err.println(s+"============");
        srcLogistics.setWarehouseId(check.getWarehouseId());
        srcLogistics.setPhoneNumber(s);
        SrcLogistics logistics = logisticsService.getOne(Wrappers.lambdaQuery(SrcLogistics.class).eq(SrcLogistics::getMaterialLotId, check.getMaterialLotId()));
        if(logistics!=null){
            return error("该物流已经去运输了");
        }
        logisticsService.save(srcLogistics);
        return success();
    }

}
